package cn.jobstart;

import cn.jobstart.common.SkMap;
import cn.jobstart.common.StringUtil;
import cn.jobstart.model.ResumeEducationalExperience;
import cn.jobstart.modelExt.ResumeBasicExt;
import cn.jobstart.modelExt.ResumeEducationalExperienceExt;
import cn.jobstart.pubInter.BaseSearch;
import cn.jobstart.pubInter.exception.SkException;
import cn.jobstart.searchExt.ResumeBasicSearchExt;
import cn.jobstart.searchExt.ResumeEducationalExperienceSearchExt;
import cn.jobstart.service.IResumeBasicService;
import cn.jobstart.service.IResumeEducationalExperienceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/**
 * @author sunke
 * @version 1.0
 * @Description
 * @date 2023-09-22 15:04
 */
@Component
@Slf4j
public class UpdateEduCommand implements CommandLineRunner {


    private static final byte[] lock = new byte[0];
    static int limit = 2000;
    private static final ExecutorService executor
            = Executors.newScheduledThreadPool(limit);


    @Resource
    private IResumeBasicService resumeBasicService;
    @Resource
    private IResumeEducationalExperienceService resumeEducationalExperienceService;
    @Value("${initZjrc}")
    private boolean initZjrc;

    public void initZjrcEduc() {


        ResumeEducationalExperienceSearchExt search = ResumeEducationalExperienceSearchExt.getInstance();
        search.setOrderBy(ResumeEducationalExperience.UPDATE_TIME);
        search.setOrderDesc(BaseSearch.ASC_ORDER);
        search.setFieldNull(ResumeEducationalExperience.RESUMES_ID);
        search.setLimit(limit);


        List<FutureTask<Integer>> futureList = new ArrayList<>();
        while (true) {

            try {
                List<ResumeEducationalExperienceExt> educationalExperienceList = resumeEducationalExperienceService.getList(search);
                if (StringUtil.isNullOrEmpty(educationalExperienceList)) {
                    break;
                }
                for (ResumeEducationalExperienceExt educationalExperience : educationalExperienceList) {


                    FutureTask<Integer> futureTask = new FutureTask<>(() -> {

                        String studentId = educationalExperience.getKzpStudentId();
                        if (StringUtil.isNullOrEmpty(studentId)) {
                            return 0;
                        }
                        ResumeBasicSearchExt resumeBasicSearch = ResumeBasicSearchExt.getInstance();
                        resumeBasicSearch.setKzpStudentIdEq(studentId);
                        ResumeBasicExt resumeBasic = resumeBasicService.getDetailBySearch(resumeBasicSearch);
                        if (StringUtil.isNullOrEmpty(resumeBasic)) {
                            return 0;
                        }

                        resumeEducationalExperienceService.updateDataById(SkMap.getInstance()
                                        .addObjToMap(ResumeEducationalExperience.RESUMES_ID, resumeBasic.getId())
                                        .addObjToMap(ResumeEducationalExperience.OLD_RESUMES_ID, resumeBasic.getOldResumesId())

                                , educationalExperience.getId()

                        );

                        return 1;
                    });

                    futureList.add(futureTask);
                    executor.submit(futureTask);

                }

                for (FutureTask<Integer> futureTask : futureList) {

                    try {
                        System.out.println(futureTask.get());


                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    }

                }
                futureList.clear();


            } catch (SkException e) {

            }


        }


    }

    @Override
    public void run(String... args) throws Exception {
        if (initZjrc) {
            //  initZjrcEduc();
        }
    }
}
